package cn.edu.nsmc.modules.sys.service.impl;

import cn.edu.nsmc.common.utils.R;
import cn.edu.nsmc.config.Constant;
import cn.edu.nsmc.modules.sys.dao.SysUserDao;
import cn.edu.nsmc.modules.sys.dao.SysWorksheetRecordDao;
import cn.edu.nsmc.modules.sys.dao.SysWorksheetTemplateDao;
import cn.edu.nsmc.modules.sys.dao.SysWorksheetTypeDao;
import cn.edu.nsmc.modules.sys.entity.SysWorksheetType;
import cn.edu.nsmc.modules.sys.service.SysStatisticsService;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SysStatisticsServiceImpl implements SysStatisticsService {
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private SysWorksheetRecordDao sysWorksheetRecordDao;
    @Resource
    private SysWorksheetTypeDao sysWorksheetTypeDao;

    @Override
    public R queryMiniAttention(Long startTime, Long endTime) {
        return null;
    }

    @Override
    public R queryMiniUse(Long startTime, Long endTime) {
        return null;
    }

    @Override
    public R queryWorksheetAudit(Long startTime, Long endTime) {
        return null;
    }

    @Override
    public void exportMiniAttention(Long startTime, Long endTime) {

    }

    @Override
    public void exportMiniUse(Long startTime, Long endTime) {

    }

    @Override
    public void exportWorksheetAudit(Long startTime, Long endTime) {

    }

    @Override
    public R queryUsersQuantity(Long startTime, Long endTime, Integer spanType) {
        //预处理span
        Map<String, Object> data = new HashMap<>();
        //使用人数信息
        //日期范围内：
        //data.put("useCnt",sysUserDao.)
        //近一段时间新增：
        Date d1 = null;
        Date d2 = null;
        if (startTime != null) {
            d1 = new Date(startTime);
        }
        if (endTime != null) {
            d2 = new Date(endTime);
        }
        data.put("useCntAll", sysUserDao.attCntAll(d1, d2));
        data.put("useCntRecently", sysUserDao.attCntRecently(spanType));
        //关注人数信息
        data.put("attCntAll", sysUserDao.attCntAll(d1, d2));
        data.put("attCntRecently", sysUserDao.attCntRecently(spanType));
        //维修人员信息
        data.put("repCntAll", sysUserDao.repCntAll(d1, d2));
        data.put("repCntRecently", sysUserDao.repCntRecently(spanType));
        return R.success("查询数据成功", data);
    }

    @Override
    public R queryWorksheetsInfo(Long d1, Long d2, Integer spanType, Integer queryType) {
        Date startTime;
        Date endTime;
        if (queryType==null){
            queryType=0;
        }
        if (d1 == null) {
            startTime = null;
        } else {
            startTime = new Date(d1);
        }
        if (d2 == null) {
            endTime = null;
        } else {
            endTime = new Date(d2);
        }
        Map<String, Object> data = new HashMap<>();
        //后端映射
        Map<Long, String> f = new HashMap<>();
        List<String> ts=new ArrayList<>();
        if (queryType == 0) {
            //审核状态 0等待处理  1审批完成（管理员处理完成） 2处理完成（维修人员处理完成） 3等待维修人员处理 4 维修工单结束  -1审核失败 -2 维修处理异常
            f= Constant.INT_TO_AUDIT_STATUS;
            ts.add("等待处理");
            ts.add("审批完成");
            ts.add("处理完成");
            ts.add("等待维修");
            ts.add("维修业务结束");
            ts.add("审核失败");
            ts.add("维修处理异常");
        } else {
            List<SysWorksheetType> list = sysWorksheetTypeDao.getList();
            for (SysWorksheetType sysWorksheetType : list) {
                f.put(sysWorksheetType.getId(), sysWorksheetType.getName());
                ts.add(sysWorksheetType.getName());
            }

        }
        List<Map<String, Object>> maps = sysWorksheetRecordDao.worksheetCntLeft(startTime, endTime, queryType);
        for (Map<String, Object> map : maps) {
            map.put("type", f.get(Long.parseLong(map.get("type").toString())));
        }
        List<Map<String, Object>> maps1 = sysWorksheetRecordDao.worksheetCntRight(startTime, endTime, spanType, queryType);
        Map<String, List<Pair>> maps2 = new HashMap<>();
        for (Map<String, Object> stringObjectMap : maps1) {
            stringObjectMap.put("type", f.get(Long.parseLong(stringObjectMap.get("type").toString())));
            maps2.computeIfAbsent(stringObjectMap.get("x").toString(), k -> new ArrayList<>());
            stringObjectMap.putIfAbsent("type", "未知类型");
            maps2.get(stringObjectMap.get("x").toString()).add(new Pair(stringObjectMap.get("type").toString(),stringObjectMap.get("y").toString()));
        }
        List<Map<String,Object>> coords = new ArrayList<>();
        maps2.forEach((key, value) -> {
            Map<String,Object> it=new HashMap<>();
            //System.out.println("Key: " + key + ", Value: " + value);
            it.put("x",key);
            for (Pair pair : value) {
                it.put(pair.getType(), pair.getY());
            }
            for (String s:ts){
                it.putIfAbsent(s, "0");
            }
            coords.add(it);
        });
        if (coords.size()>1){
            final SimpleDateFormat sdf;
            if (spanType==0){
                 sdf=new SimpleDateFormat("yyyy-MM-dd");
            }else if (spanType==1){
                sdf=new SimpleDateFormat("yyyy-MM");
            }else {
                sdf=new SimpleDateFormat("yyyy");
            }
            coords.sort((o1, o2) -> {
                try {
                    Date date1 = sdf.parse((String) o1.get("x"));
                    Date date2 = sdf.parse((String) o2.get("x"));
                    return date1.compareTo(date2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            });
        }
        data.put("worksheetCntLeft", maps);
        data.put("worksheetCntRight", maps1);
        data.put("d", coords);
        return R.success("查询数据成功", data);
    }
    @Data
    @AllArgsConstructor
    class Pair{
        private String type;
        private String y;
    }
}
